import store from '@/store'
import { App } from 'vue'
// 自定义权限指令
export default (app: App<Element>) => {
    app.directive('permission', (el: any, binding: any) => {
        const { value } = binding
        let st:any  = store
        const roles = st && st.state.user.roles
        if (value && value instanceof Array && value.length > 0) {
            const permissionRoles = value
            const hasPermission = roles.some((role: any) => {
                return permissionRoles.includes(role) || permissionRoles.includes('all')
            })

            if (!hasPermission) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error(`使用方式： v-permission="['admin','editor']"`)
        }

    })
}
